<?php
/**
 * Created by PhpStorm.
 * User: mrlee
 * Date: 2019-02-25
 * Time: 22:20
 */

namespace app\admin\controller;

use app\common\controller\Helpinfo;
use app\common\controller\WxCardDate;
use app\common\model\CardCodeUser;
use app\common\model\CardCounted;
use app\common\model\CardGeneral;
use app\common\model\CardMember;
use app\common\model\House;
use app\common\model\Unit;
use think\Controller;
use think\Request;
use think\Session;
use Wechat\Wxobjopen;

class WxCardus extends AdminBase
{



    //会员卡添加
    public function cardus_create($param)
    {

        $meinfo = $this->getcacheinfo($param['token']);
//        $meinfo['sysid'] = 2;
        $CardDate = new WxCardDate();
        $data = $CardDate->carduscreate($param,1);
        return $data;
        $optionwx = $this->optionwx($meinfo['sysid']);
        $WxObj = new Wxobjopen($optionwx);
        $res = $WxObj->createUserCard($data);
//        $res['errcode'] = 0;
//        $res['card_id'] = 'asdasdjkahsuidhaskhdksa';
        if ($res['errcode'] == 0){

            $datas = $CardDate->carduscreate($param,2,$data,$res);
//            return $datas;
            $datas['sysid'] = $meinfo['sysid'];
            $datas['rid'] = $param['rid'];
            $datas['param_info'] = json_encode($param,JSON_UNESCAPED_UNICODE);
            $datas['card_info'] = json_encode($data,JSON_UNESCAPED_UNICODE);
            $CardMember = new CardMember();
            $add=$CardMember->allowField(true)->save($datas);
            $re_param['res'] = $res;
            if ($add){
                $re_param['return_code'] = "SUCCESS";
                $re_param['return_msg'] = "成功";
            } else {
                $re_param['return_code'] = "FAIL";
                $re_param['return_msg'] = "保存失败";
            }
        }else{
            $re_param['return_code'] = "FAIL";
            $re_param['return_msg'] = "创建失败,".$res['errmsg'];
        }
        return $re_param;
    }

    //会员卡修改
    public function cardus_edit($param)
    {
        //获取登陆信息
        $meinfo = $this->getcacheinfo($param['token']);
        //获取会员卡信息
        $CardMember = new CardMember();
        $maps['sysid'] = $meinfo['sysid'];
        $maps['id'] = $param['id'];
        $cardinfo = $CardMember->where($maps)->find();
        if ($cardinfo) {
            //处理修改数据
            $CardDate = new WxCardDate();
            $param['card_id'] = $cardinfo['card_id'];
            $data = $CardDate->cardusedit($param, 1);
//            return json_decode($data['member_card']['base_info'], true);
            //发布修改
            $optionwx = $this->optionwx($meinfo['sysid']);
            $WxObj = new Wxobjopen($optionwx);
            $res = $WxObj->updateUserCard($data);
            if ($res['errcode'] == 0) {
                //成功后保存结果
                $map['sysid'] = $meinfo['sysid'];
                $map['id'] = $param['id'];
                $datas = $CardDate->cardusedit($param, 2, $data);
                $datas['param_info'] = json_encode($param,JSON_UNESCAPED_UNICODE);
                $datas['card_info'] = json_encode($data, JSON_UNESCAPED_UNICODE);
                $datas['rid'] = $param['rid'];
                $add = $CardMember->save($datas, $map);
                if ($add) {
                    $re_param['return_code'] = "SUCCESS";
                    $re_param['return_msg'] = "成功";
                } else {
                    $re_param['return_code'] = "FAIL";
                    $re_param['return_msg'] = "保存失败";
                }
            } else {
                $re_param['return_code'] = "FAIL";
                $re_param['return_msg'] = "修改失败," . $res['errmsg'];
            }
        }else{
            $re_param['return_code'] = "FAIL";
            $re_param['return_msg'] = "修改失败";
        }
        return $re_param;
    }

    //会员卡详情
    public function cardus_info($param)
    {
        //END
        $meinfo = $this->getcacheinfo($param['token']);
        $map['sysid'] = $meinfo['sysid'];
        $map['id'] = $param['id'];
//        return $map;
        $CardMember = new CardMember();
        $info=$CardMember->where($map)->find();
        if ($info) {
            $re_param['return_code'] = "SUCCESS";
            $re_param['return_msg'] = "获取成功";
            $re_param['data']['items'] = _unsetNull($info);
        } else {
            $re_param['return_code'] = "FAIL";
            $re_param['return_msg'] = "获取失败";
        }
        return $re_param;
    }

    //会员卡删除
    public function cardus_del($param)
    {
        //END   delUserCard
        $meinfo = $this->getcacheinfo($param['token']);
        $CardMember = new CardMember();
        $map['sysid'] = $meinfo['sysid'];
        $map['id'] = $param['id'];
        $cardinfo = $CardMember->where($map)->field('card_id,id')->find();
        if ($cardinfo) {
            $data['card_id'] = $cardinfo['card_id'];
            $optionwx = $this->optionwx($meinfo['sysid']);
            $WxObj = new Wxobjopen($optionwx);
            $res = $WxObj->delUserCard($data);
            $re_param['res']  = $res;
            $res['errcode'] = 0;
            if ($res['errcode'] == 0) {
                $datas['is_del'] = 2;
//                $datas['del_info'] = json_encode($data, JSON_UNESCAPED_UNICODE);
                $info = $CardMember->save($datas, $map);
                if ($info) {
                    $re_param['return_code'] = "SUCCESS";
                    $re_param['return_msg'] = "成功";
                } else {
                    $re_param['return_code'] = "FAIL";
                    $re_param['return_msg'] = "删除失败";
                }
            } else {
                $re_param['return_code'] = "FAIL";
                $re_param['return_msg'] = "删除失败," . $res['errmsg'];
            }
        }else{
            $re_param['return_code'] = "FAIL";
            $re_param['return_msg'] = "删除异常";
        }
        return $re_param;
    }

    /*
    * 卡券操作
    *
    */
    //卡券列表
    public function cards_list($param)
    {
        $map = [];
        $map = $this->getmysearch($param,'a');
        $map['is_del'] = 1;
        if (!empty($param['type'])){
            //已发布
            if ($param['type'] == 'cardslist'){
                $map['is_put'] = 1;
            }
            //未发布
            if ($param['type'] == 'draftslist'){
                $map['is_put'] = 2;
            }
            //已过期
            if ($param['type'] == 'overlist'){
                $map['is_put'] = 1;
                $map['end_timestamp'] = ['LT',time()];
            }
        }
        $CardGeneral = new CardGeneral();
        $list = $CardGeneral->where($map)->select();
        $total = $CardGeneral->where($map)->count();
        if ($list){
            foreach ($list as $k=>$v){
                $list[$k]['card_type_ch'] = getcardtype($v['card_type']);
            }
            $re_param['return_code'] = "SUCCESS";
            $re_param['return_msg'] = "成功";
            $re_param['data']['items'] = $list;
            $re_param['data']['total'] = $total;
        } else {
            $re_param['return_code'] = "SUCCESS";
            $re_param['return_msg'] = "成功";
            $re_param['data']['items'] = $list;
            $re_param['data']['total'] = $total;
        }
        return $re_param;
    }


    //发布卡券
    public function cards_add($param){
        $meinfo = $this->getcacheinfo($param['token']);
        $map['id'] = $param['id'];
        $map['sysid'] = $meinfo['sysid'];
        $CardGeneral = new CardGeneral();
        $info = $CardGeneral->where($map)->find();
        if ($info) {
            $data = json_decode($info['card_info'],true);
            //初始化微信API
            $optionwx = $this->optionwx($meinfo['sysid']);
            $WxObj = new Wxobjopen($optionwx);
            $res = $WxObj->createUserCard($data);
            if ($res['errcode'] == 0) {
                $datas['card_id'] = $res['card_id'];
                $datas['is_put'] = 1;
                $CardGeneral->allowField(true)->save($datas, $map);
                $re_param['return_code'] = "SUCCESS";
                $re_param['return_msg'] = "发布成功";
            } else {
                //错误提示翻译
                $Helpinfo = new Helpinfo();
                $errmsg = $Helpinfo->gethelpinfo(0, $res['errcode'], $res['errmsg']);
                //END
//                        var_dump($res);
                $datas['errmsg'] = $errmsg;
                $CardGeneral->allowField(true)->save($datas, $map);
                $re_param['return_code'] = "FAIL";
                $re_param['return_msg'] = "发布失败".$errmsg;
            }
        }else{
            $re_param['return_code'] = "FAIL";
            $re_param['return_msg'] = "发布失败";
        }
        return $re_param;

    }

    //卡券修改
    public function cards_edit($param)
    {
        $meinfo = $this->getcacheinfo($param['token']);
        $CardDate = new WxCardDate();
        $data = $CardDate->cardscreate($param,1);

        $datas = $CardDate->cardscreate($param,2);
//        $datas['sysid'] = $meinfo['sysid'];
//        $datas['rid'] = 4;
        $map['id'] = $param['id'];
        $map['sysid'] = $meinfo['sysid'];
        $datas['param_info'] = json_encode($param,JSON_UNESCAPED_UNICODE);
        $datas['card_info'] = json_encode($data,JSON_UNESCAPED_UNICODE);
        $CardGeneral = new CardGeneral();
        $add=$CardGeneral->save($datas,$map);
        if ($add){
            $re_param['return_code'] = "SUCCESS";
            $re_param['return_msg'] = "修改成功";
        } else {
            $re_param['return_code'] = "FAIL";
            $re_param['return_msg'] = "保存失败";
        }

        return $re_param;
    }

    //卡券详情
    public function cards_info($param)
    {
        //END
        $meinfo = $this->getcacheinfo($param['token']);
        $map['sysid'] = $meinfo['sysid'];
        $map['id'] = $param['id'];
        $CardGeneral = new CardGeneral();
        $info=$CardGeneral->where($map)->find();
        if ($info) {
            $re_param['return_code'] = "SUCCESS";
            $re_param['return_msg'] = "获取成功";
            $re_param['data']['items'] = _unsetNull($info);
        } else {
            $re_param['return_code'] = "FAIL";
            $re_param['return_msg'] = "获取失败";
        }
        return $re_param;
    }

    //卡券详情
    public function cards_del($param)
    {
        //END   delUserCard
        $meinfo = $this->getcacheinfo($param['token']);
        $data = $param;
        $optionwx = $this->optionwx($meinfo['sysid']);
        $WxObj = new Wxobjopen($optionwx);
        $res = $WxObj->delUserCard($data);
        if ($res['errcode'] == 0){
            $map['sysid'] = $meinfo['sysid'];
            $map['id'] = $param['id'];
            $datas['info'] = json_encode($data,JSON_UNESCAPED_UNICODE);
            $CardMember = new CardMember();
            $info=$CardMember->where($map)->delete();
            if ($info){
                $re_param['return_code'] = "SUCCESS";
                $re_param['return_msg'] = "成功";
            } else {
                $re_param['return_code'] = "FAIL";
                $re_param['return_msg'] = "删除失败";
            }
        }else{
            $re_param['return_code'] = "FAIL";
            $re_param['return_msg'] = "删除失败,".$res['errmsg'];
        }
        return $re_param;
    }



    
}